package org.answer.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.answer.dao.MemberDao;
import org.answer.entity.PageResult;
import org.answer.entity.QueryPageBean;
import org.answer.pojo.Member;
import org.answer.service.MedicalUploadService;
import org.answer.service.MemberService;
import org.answer.utils.MD5Utils;
import org.answer.utils.QiniuUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Autowired
    private MedicalUploadService medicalUploadService;

    @Override
    public void add(Member member) {

        //用户注册可能有密码，有密码就需要进行加密的处理
        String password = member.getPassword();
        if(password != null) {
            //用户设置了密码，加密
            password =MD5Utils.md5(password);
            //建加密后的密码设置回对象里进行添加
            member.setPassword(password);
        }

        String fileNumber = member.getFileNumber();

        //调用方法，判断档案号时候存在
        boolean existFileNumber = ifExistFileNumber(fileNumber);

        if (existFileNumber){
            throw new RuntimeException("档案号已存在");
        }
        //调用方法进行会员的添加
        memberDao.add(member);
    }

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }


    //根据月份查询会员数量
    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {//2018.05
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";//2018.05.31
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //设置分页查询,基于mybatis框架
        PageHelper.startPage(currentPage,pageSize);

        //select * from t_checkitem limit 0,10
        Page<Member> page = memberDao.selectByCondition(queryString);

        long total = page.getTotal();

        List<Member> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public void delete(Integer id) {

        //查询这个用户有没有预约
        Long count = memberDao.getOrder(id);
        if (count != null && count > 0){
            //表示这个用户已经预约了，禁止删除
            throw new RuntimeException("用户已经预约了，禁止删除");
        }

        List<String> list = medicalUploadService.downloadHealthFrom(id);
        if(list.size()>0){
            for (String s : list) {
                String[] split = s.split("com/");
                //删除七牛云中数据
                QiniuUtils.deleteFileFromQiniu(split[1]);
            }
        }
        //删除healthForm
        medicalUploadService.delete(id);

        //通过id删除用户
        memberDao.deleteById(id);
    }

    /**
     * 修改用户信息
     *
     * @param member 修改后的信息
     */
    @Override
    public void update(Member member) {
        memberDao.edit(member);
    }

    @Override
    public List<Member> memberExcel() {
        //查出来的所有的数据
        List<Member> list = memberDao.findAll();
        return list;
    }



    /**
     * 传入一个档案号，判断档案号是否存在
     * @param fileNumber 档案号的名字
     * @return  存在就返回true
     */
    private boolean ifExistFileNumber(String fileNumber) {

        //不存在返回false
        boolean flag = false;
        if (fileNumber != null){
            //查询所有的档案号，看有没有重复的
            List<Member> all = memberDao.findAll();
            for (Member m : all) {
                //数据库里面的每一个档案编号
                String name = m.getFileNumber();
                //判断
                if (fileNumber.equals(name) || fileNumber == name){
                    //存在将状态改为true
                    return flag = true;
                }
            }
        }
        return false;
    }
}
